let brain = require("brain.js");
let fs = require("fs");
let data = require("../res/res.json");
let path = require("path");
let network = new brain.NeuralNetwork({
  inputSize: 33,
  hiddenLayers: [80, 33],
  outputSize: 6,
});
// data = JSON.parse(data);
// console.log(data);
let trainData = []; // 训练数据

for (let item in data) {
  let o = item - 0 + 1;
  if (data[o] && item >= 2023000) {
    trainData.push({
      input: data[item].wzjred.map((i) => i),
      output: data[o].red.map((i) => i / 100),
    });
  }
}

network.train(trainData, {
  // learningRate: 0.1,
  errorThresh: 0.000001,
  iterations: 20000, // 训练迭代次数
  log: true, // 是否打印训练日志
  logPeriod: 500, // 日志打印间隔
  learningRate: 0.05,
  momentum: 0.1,
  // callbackPeriod: 10,
  // timeout: Infinity,
});
const jsonstr = network.toJSON();
// net.fromJSON(json);

fs.writeFile(
  path.join(__dirname, "./model.json"),
  JSON.stringify(jsonstr, null, 2),
  { flag: "w" },
  () => {}
);

// const output = network.run("CSS flex for example layouts"); // 前端任务：用于复杂布局的 CSS Grid
// const output2 = network.run("optimizing SQL queries"); // 后端任务：优化 SQL 查询
// console.log(output);
// console.log(output2);
